package com.example.demo.web;


import com.example.demo.dao.StudentMapper;
import com.example.demo.entity.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("student")
public class StudentController {

    @Autowired
    StudentMapper studentMapper;

    @GetMapping("list")
    public String list(ModelMap map){
        map.put("userList", studentMapper.list());
        return "/student/list";
    }


    @GetMapping("delete/{id}")
    public String list(@PathVariable("id") Integer id){
        studentMapper.delete(id);
        return "redirect:/student/list";
    }


    @GetMapping("add")
    public String add(){
        return "/student/add";
    }


    @PostMapping("add")
    public String add(Student student){
        studentMapper.save(student);
        return "redirect:/student/list";
    }


    @GetMapping("edit/{id}")
    public String edit(@PathVariable("id") Integer id,ModelMap map){
        map.put("user", studentMapper.findById(id));
        return "/student/edit";
    }


    @PostMapping("edit")
    public String edit(Student student){
        studentMapper.update(student);
        return "redirect:/student/list";
    }
}
